package com.zidian.study.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zidian.study.bean.QuestionListBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.zidian.student.trucks.R;

/**
 * Created by Administrator on 2016/8/24 0024.
 * 提问列表适配器
 */
public class InteractionRecyclerAdapter extends Adapter<ViewHolder> {
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private Context context;
    private List<QuestionListBean.DataBean> data;
    private boolean hasMore = true;

    public void setHasMore(boolean hasMore) {
        this.hasMore = hasMore;
    }

    public InteractionRecyclerAdapter(Context context, List<QuestionListBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return data.size() == 0 ? 0 : data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_interaction_ask, parent,
                    false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_interaction_ask_foot, parent,
                    false);
            return new FootViewHolder(view);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {

            ((ItemViewHolder) holder).askCourse.setText(data.get(position).getIssueObject());
            ((ItemViewHolder) holder).askPersonName.setText(data.get(position).getIssuePersonName());
            ((ItemViewHolder) holder).askTime.setText(dateToStamp(data.get(position).getIssueTime()));
            ((ItemViewHolder) holder).askTitle.setText(data.get(position).getIssueText());


            Glide.with(context)
                    .load(data.get(position).getIssuePersonHeadPortrait())
                    .placeholder(R.drawable.student)
                    .centerCrop()
                    .into(((ItemViewHolder) holder).askPersonImage);
            //设置问题列表的图标
            if (data.get(position).getIssueUrl() != null &data.get(position).getIssueUrl().size() != 0) {
                ((ItemViewHolder) holder).askQuestionImage.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(data.get(position).getIssueUrl().get(0).getUrl())
                        .placeholder(R.mipmap.case_news_bg_93)
                        .into(((ItemViewHolder) holder).askQuestionImage);
            }else {
                ((ItemViewHolder) holder).askQuestionImage.setVisibility(View.GONE);
            }
            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemClick(holder.itemView, position);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemLongClick(holder.itemView, position);
                        return false;
                    }
                });
            }
        } else if (holder instanceof FootViewHolder) {
            if (hasMore) {
                ((FootViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
                ((FootViewHolder) holder).loadText.setText("正在加载");
            } else {
                ((FootViewHolder) holder).progressBar.setVisibility(View.GONE);
                ((FootViewHolder) holder).loadText.setText("加载完成");
            }
        }
    }


    static class ItemViewHolder extends ViewHolder {

        TextView askCourse;
        TextView askTitle;
        TextView askTime;
        TextView askPersonName;
        ImageView askPersonImage;
        ImageView askQuestionImage;

        public ItemViewHolder(View view) {
            super(view);

            askCourse = (TextView) view.findViewById(R.id.ask_class_name);
            askTitle = (TextView) view.findViewById(R.id.ask_title);
            askTime = (TextView) view.findViewById(R.id.ask_time);
            askPersonImage = (ImageView) view.findViewById(R.id.ask_use_image);
            askPersonName = (TextView) view.findViewById(R.id.ask_user_name);
            askQuestionImage = (ImageView) view.findViewById(R.id.ask_question_image);
        }
    }

    static class FootViewHolder extends ViewHolder {


        ProgressBar progressBar;
        TextView loadText;

        public FootViewHolder(View view) {
            super(view);
            progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
            loadText = (TextView) view.findViewById(R.id.load_text);
        }
    }

    public static String dateToStamp(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        res = simpleDateFormat.format(new Date(Long.parseLong(String.valueOf(s))));
        return res;
    }
}
